package hlcache

import (
	"context"
	"fmt"
	"math/rand"
	"time"
)

func main() {
	var err error
	var ctx context.Context
	var cancel context.CancelFunc
	var cacheObj *CacheObj
	var tmpI int
	var tmpDataKey string
	var tmpDataValue string
	ctx, cancel = context.WithCancel(context.Background())
	err, cacheObj = EnableLocalCache(ctx, 4096)
	if err != nil {
		fmt.Println("Error:", err.Error())
		cancel()
		return
	}
	for tmpI = 0; tmpI < 100; tmpI++ {
		tmpDataKey = getRandomText(10 + tmpI)
		tmpDataValue = getRandomText(64 + tmpI)
		err = cacheObj.Set(tmpDataKey, tmpDataValue, 2)
		if err != nil {
			fmt.Println("SetError:", err.Error())
		}
	}
	fmt.Println("Debug:", cacheObj.GetSize())
	cancel()
	time.Sleep(1 * time.Second)
}

// getRandomText 生成随机数
func getRandomText(num int) string {
	var txtChars = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*-_=+{[}]\|:;,.<>/?`
	textNum := len(txtChars)
	text := ""
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < num; i++ {
		tmpIdx := r.Intn(textNum)
		text = text + string(txtChars[tmpIdx])
	}
	return text
}
